  <template>
  <div class="home">
    <div class="content">
      <div v-for="news in stories" :key="news.id" class="news-card">
        <a :href="news.url" target="_blank">
          <!-- 接口返回两个图片的数据类型不同，一个是数组images，一个是字符串image -->
          <div class="news-front"> 
            <el-image
              style="width: 100%; height: 100%"
              :src="news.image || news.images[0]"
              >
              <div slot="error" class="image-slot" style="background:white;height:100%">
                <el-image
                  :src="require('../assets/image/404.jpg')"
                  lazy>
                </el-image>
              </div>
            </el-image>
            <p class="news-title">{{ news.title }}</p>
          </div>
          <div class="news-back">
            <div>
              <p>{{ news.title }}</p>
              <p>>{{ news.hint }}</p>
              <button class="news-button">阅读</button>
            </div>
          </div>
        </a>
      </div>
    </div>
  </div>
</template>

<script>
import { getDate } from '../utils/date';
import { mapState } from 'vuex';

export default {
  name: "home",
  data () {
    return {
      now: getDate(new Date(),'')
    }
  },
  created(){
    // 在create时，判断vuex里的news是否当天，非当天即重新发起请求获取
    if(this.now != this.newsDate){
      this.$store.dispatch('getZhiHuNews');
    }
  },
  computed:{
    ...mapState({
      newsDate: state => state.news.newsDate,
      stories: state => state.news.stories,
      topStories: state => state.news.topStories
    })
  },
  methods:{
    nofind(){
      console.log('error');
    }
  }
};
</script>

<style scoped>
.home{
  padding-top: 60px;
  font-size: 18px;
  line-height: 1.5;
  font-weight: 300;
  color: #333;
}
.home p{
  padding: 15px;
}
.home a{
  text-decoration: none;
  color: inherit;
}
.content {
  display: flex;
  margin: 0 auto;
  justify-content: center;
  align-items: center;
  flex-wrap: wrap;
}
.content *, *:before, *:after {
  box-sizing: border-box;
}
.news-card {
  color: inherit;
  cursor: pointer;
  width: calc(30% - 2rem);
  min-width: calc(30% - 2rem);
  height: 400px;
  min-height: 400px;
  perspective: 1000px;
  margin: 2rem;
  position: relative;
}
@media screen and (max-width: 800px) {
  .news-card {
    width: calc(50% - 2rem);
  }
}
@media screen and (max-width: 500px) {
  .news-card {
    width: 100%;
  }
}
.news-front ,
.news-back
{
  display: flex;
  border-radius: 6px;
  background-position: center;
  background-size: cover;
  text-align: center;
  justify-content: center;
  align-items: center;
  position: absolute;
  height: 100%;
  width: 100%;
  -webkit-backface-visibility: hidden;
  backface-visibility: hidden;
  transform-style: preserve-3d;
  transition: ease-in-out 600ms;
}
.news-title {
  position: absolute;
  top: 45%;
  font-size: 1.618rem;
  font-weight: 600;
  color: #00B0FB;
  overflow: hidden;
}
.news-front:before {
  position: absolute;
  display: block;
  content: '';
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(135deg, #1a9be6, #1a57e6);
  opacity: .25;
  z-index: -1;
}
.news-card:hover .news-front  {
  transform: rotateY(180deg);
}
.news-card:nth-child(even):hover .news-front  {
  transform: rotateY(-180deg);
}
.news-back {
  background: #fff;
  transform: rotateY(-180deg);
  padding: 0 2em;
}
.news-card:hover .news-back {
  transform: rotateY(0deg);
}
.news-card:nth-child(even) .news-back {
  transform: rotateY(180deg);
}
.news-card:nth-child(even):hover .news-back {
  transform: rotateY(0deg);
}
.news-button {
  transform: translateZ(40px);
  cursor: pointer;
  -webkit-backface-visibility: hidden;
  backface-visibility: hidden;
  font-weight: bold;
  color: #fff;
  padding: .5em 1.5em;
  border-radius: 100px;
  font: inherit;
  background: linear-gradient(135deg, #1a9be6, #1a57e6);
  border: none;
  position: relative;
  transform-style: preserve-3d;
  transition: 300ms ease;
  outline:none;
}
.news-button:before {
  transition: 300ms ease;
  position: absolute;
  display: block;
  content: '';
  transform: translateZ(-40px);
  -webkit-backface-visibility: hidden;
  backface-visibility: hidden;
  height: calc(100% - 20px);
  width: calc(100% - 20px);
  border-radius: 100px;
  left: 10px;
  top: 16px;
  box-shadow: 0 0 10px 10px rgba(26, 87, 230, 0.25);
  background-color: rgba(26, 87, 230, 0.25);
}
.news-button:hover {
  transform: translateZ(55px);
}
.news-button:hover:before {
  transform: translateZ(-55px);
}
.news-button:active {
  transform: translateZ(20px);
}
.news-button:active:before {
  transform: translateZ(-20px);
  top: 12px;
}
</style>